layui.define(['jquery', 'setter', 'mynotice'], function (exports) {
    var $ = layui.$;
    var setter = layui.setter;
    var mynotice = layui.mynotice;

    var $ax = function (url, type, success, error) {
        this.url = setter.baseServer + setter.contextPath + url;
        this.type = type;
        this.data = {};
        this.dataType = "json";
        this.async = true;
        this.success = success;
        this.error = error;
        this.contentType = "application/json;charset=utf-8";
    };

    $ax.prototype = {
        start: function () {
            var me = this;
            var result = "";
            if (this.url.indexOf("?") === -1) {
                this.url = this.url;
            } else {
                this.url = this.url;
            }

            $.ajax({
                type: me.type,
                url: me.url,
                dataType: me.dataType,
                contentType: me.contentType,
                async: me.async,
                data: me.data,
                beforeSend: function (data) {
                	data.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
                },
                success: function (data) {
                	layer.closeAll('loading');
                    if(data.code === 0 && me.success !== undefined){
                    	me.success(data);
                    }else if(me.error !== undefined){
                    	if(data.code === 401){
                    		setter.removeToken();
                            location.replace('/login');
                    	}else{
                    		me.error(data);
                    	}
                    }
                },
                error: function (data) {
                	layer.closeAll('loading');
                	var res = {};
                	res['code'] = data.status;
                	res['data'] = null;
                	if(data.status === 503){
                		res['msg'] = '请求失败，请稍后再试';
                	}else if(data.status === 401){
                		setter.removeToken();
                		res['msg'] = '登录已失效！';
                        location.replace('login.html');
                	}
                	if(me.error !== undefined){
                		if(data.responseJSON !== undefined){
                			res['msg'] = data.responseJSON.msg;
                    	}else{
                    		res['msg'] = "请求失败，请稍后再试";
                    	}
                		me.error(res);
                	}else{
                		mynotice.error(data.statusText);
                	}
                }
            });
            return result;
        },

        set: function (key, value) {
            if (typeof key === "object") {
                for (var i in key) {
                    if (typeof i === "function")
                        continue;
                    this.data[i] = key[i];
                }
            } else {
                this.data[key] = (typeof value === "undefined") ? $("#" + key).val() : value;
            }
            return this;
        },

        setData: function (data) {
            this.data = data;
            return this;
        },

        clear: function () {
            this.data = {};
            return this;
        }
    };

    exports('ax', $ax);
});